/**
 * 1688
 */
class ChromePlugin1688 extends ChromePlugin {
    constructor() {
      super();
      this.handleExport = this.handleExport.bind(this);
    }
    install() {
      console.log("检测到当前为1688,自动安装1688插件")
      this.initUI()
    }
    initUI() {
        // 如果当前是
        // 退款url
        if (location.hostname === 'work.1688.com') {
            this.exportRefundUrl =  $('iframe').attr('src')
            location.href = `http:${this.exportRefundUrl}`
        }
        // 批量导出按钮
        let exportBtn = $("<button>");
        exportBtn.text("批量导出");
        exportBtn.addClass(
          "lang-button refund-search-button"
        );
        exportBtn.css("margin-left", "10px");
        exportBtn.on("click", this.handleExport);
        $(".lang-button").after(exportBtn);
    }
    // 批量导出导出按钮跳转事件
    async handleExport(event) {
      // 创建进度条执行任务
      createEyProgress()
      setEyProgressWithTitle(0, "订单导出")
      showEyProgress()
      // 先获取到有多少页
      const total = $('.pages-total').text()
      // 获取到当前筛选的条件
      let url = location.href
      // 结果集
      let result = []
      // updateUrlParameter 
      // 循环总条数发送ajax请求
      for (let page = 1; page <= total; page++) {
        // 导出所有页 
        const { data } = await axios.get(updateUrlParameter(url, "page", page))
        // 更新进度条进度
        setEyProgress(Math.round(page / total * 100))
        // 创建dom挂载到页面
        let dataDiv = $("<div>")
        dataDiv.attr('id', 'ey-hidden')
        dataDiv.html(data)
        $("body").append(dataDiv)
        // 解析数据
        const refundListDom = $("#ey-hidden .refund-list-container .unit.unit-refund-item")
        refundListDom.each(function() {
          // 退款单号
          const refundOrderNumber = $(this).find('.refund-item-header.fd-clr .fd-left span:first').text().split("：")[1]
          // 订单号
          const orderNumber = $(this).find('.refund-item-header.fd-clr .fd-left span').eq(1).text().split("：")[1]
          // 货品已付金额
          const shopPayAmount = $(this).find('table tbody tr td:eq(3) .amount').text().trim()
          // 退款金额(包含运费部分)
          const refundAmount = $(this).find('table tbody tr td:eq(4) .amount').text() + $(this).find('table tbody tr td:eq(4) p:eq(1)').text()
          // 退款退货状态
          const refundState = $(this).find('table tbody tr td:eq(5) p').text()
          // 申请时间
          const applyTime = $(this).find('.refund-item-header.fd-clr .fd-right span:first').text().split("：")[1]
          result.push([refundOrderNumber, orderNumber, shopPayAmount, refundAmount, refundState, applyTime])
        })
        dataDiv.remove()
      }
      // 关闭进度条
      hideEyProgress()
      // 写出excel
      exportToCSV(
        ["退款单号", "订单号", "货品已付金额", "退款金额", "退款退货状态", "申请时间"],
        result,
        "1688退货订单数据"
      );
      alert("导出成功")
    }
    
    
    
}
// https://dispute.1688.com/refund/buyerRefundList.htm?disputeRequest=0&hasworksheet=0&disputeType=0&disputeStatus=all&startDate=2024-04-01&endDate=2024-04-30&page=2